﻿/*
// CHANGE LOG
mod 16 jun 07 - ddb
	ver 1.02
	changed:
		
	added:
		toString() method
		className_str:String
		version_str:String

*/
/*
// METHOD TABLE:
// TableCell methods
//
function TableCell(p_table_obj:Object) {
public function create(p_cellData_obj:Object):MovieClip {
private function drawRectangle(theClip:MovieClip, cornerRadius:Array):Void {
private function toString():Void{
*/
class com.utils.TableCell {
	private var className_str:String;
	private var version_str:String;
	// properties
	private var root:MovieClip;
	private var cellText:String;
	private var textFieldWidth:Number;
	private var cornerRadius:Array;
	private var whitespace:Number;
	private var fillColor:Number;
	private var strokeColor:Number;
	private var strokeWidth:Number;
	private var Clip:MovieClip;
	private var Text:TextField;
	private var coords:Array;
	// constructor
	function TableCell() {
		className_str = "  TableCell";
		version_str = "v1.02";
		//trace(this);
		// empty
	}
	public function create(p_cellData_obj:Object):MovieClip {
		//trace("TableCell.create()");
		// validate paramaters
		if (p_cellData_obj.root == null || p_cellData_obj.root == undefined) {
			trace("    error: root is undefined");
			// fail
			return;
		}
		if (p_cellData_obj.text == null || p_cellData_obj.text == undefined) {
			p_cellData_obj.text = "error: text is undefined";
		}
		if (p_cellData_obj.cellWidth == null || p_cellData_obj.cellWidth == undefined) {
			p_cellData_obj.cellWidth = 200;
		}
		if (p_cellData_obj.cornerRadius == null || p_cellData_obj.cornerRadius == undefined) {
			p_cellData_obj.cornerRadius = new Array(0,0,0,0);
		}
		if (p_cellData_obj.whitespace == null || p_cellData_obj.whitespace == undefined) {
			p_cellData_obj.whitespace = 4;
		}
		if (p_cellData_obj.fillColor == null || p_cellData_obj.fillColor == undefined) {
			p_cellData_obj.fillColor = 0xffffcc;
		}
		if (p_cellData_obj.strokeColor == null || p_cellData_obj.strokeColor == undefined) {
			p_cellData_obj.strokeColor = 0x000000;
		}
		if (p_cellData_obj.strokeWidth == null || p_cellData_obj.strokeWidth == undefined) {
			p_cellData_obj.strokeWidth = 1;
		}
		// assign properties from paramaters 
		root = p_cellData_obj.root;
		cellText = p_cellData_obj.text;
		textFieldWidth = p_cellData_obj.cellWidth;
		cornerRadius = p_cellData_obj.cornerRadius;
		whitespace = p_cellData_obj.whitespace;
		fillColor = p_cellData_obj.fillColor;
		strokeColor = p_cellData_obj.strokeColor;
		strokeWidth = p_cellData_obj.strokeWidth;
		coords = p_cellData_obj.coords;
		/*
		trace("root:"+root);
		trace("cellText:"+cellText);
		trace("textFieldWidth:"+textFieldWidth);
		trace("cornerRadius:"+cornerRadius);
		trace("whitespace:"+whitespace);
		trace("fillColor:"+fillColor);
		trace("strokeColor:"+strokeColor);
		trace("strokeWidth:"+strokeWidth);
		trace("coords:"+coords);
		*/
		// create mc
		Clip = root.createEmptyMovieClip("cell"+p_cellData_obj.id+"_mc", p_cellData_obj.id);
		Clip.suicide = function(){
			//trace("suicide()");
			removeMovieClip(this);
		};
		// create textfield
		Clip.createTextField("cell"+p_cellData_obj.id+"_txt", 1, whitespace, whitespace, textFieldWidth, 200);
		Text = Clip["cell"+p_cellData_obj.id+"_txt"];
		// set textField properties
		Text.type = "dynamic";
		Text.border = false;
		Text.background = false;
		Text.password = false;
		Text.multiline = true;
		Text.html = true;
		Text.embedFonts = false;
		Text.selectable = false;
		Text.wordWrap = true;
		Text.condenseWhite = false;
		Text.restrict = null;
		Text.variable = null;
		Text.maxChars = null;
		Text.styleSheet = undefined;
		Text.tabEnabled = undefined;
		Text.autoSize = "left";
		// set text
		Text.htmlText = cellText;
		// adjust textField height
		Clip._height = Text.textHeight+4;
		// had to add 4 to fit last line (??)
		// adjust textField pos
		// (rumor: textField displays fuzzy when not on whole pixel)
		// ensure textField is on whole pixel 
		Clip._x = Math.round(Clip._x);
		Clip._y = Math.round(Clip._y);
		// set mc dims
		Clip.innerWidth = textFieldWidth+(whitespace*2);
		if (p_cellData_obj.cellHeight!= undefined) {
			Clip.innerHeight = p_cellData_obj.cellHeight;
		} else {
			Clip.innerHeight = Clip._height+(whitespace*2);
		}
		// create stroke/fill
		drawRectangle(Clip, cornerRadius);
		// position mc
		if (coords.length == 2) {
			Clip._x = coords[0];
			Clip._y = coords[1];
		}
		// return reference to mc
		return Clip;
	}
	private function drawRectangle(theClip:MovieClip, cornerRadius:Array):Void {
		// trace("drawRectangle()");
		if (strokeWidth == undefined) strokeWidth = 1;
		if (strokeColor == undefined) strokeColor = 0xFFFFFF;
		if (fillColor == undefined) fillColor = 0xFFFFFF;
		if (theClip.innerWidth == undefined) theClip.innerWidth = Stage.width;
		if (theClip.innerHeight == undefined) theClip.innerHeight = Stage.height;
		// set stroke
		theClip.lineStyle(strokeWidth, strokeColor, 100);
		// set fill
		theClip.beginFill(fillColor);
		// upper left corner
		theClip.moveTo(0, cornerRadius[0]);
		theClip.curveTo(0, 0, cornerRadius[0], 0);
		// top side
		theClip.lineTo((theClip.innerWidth-cornerRadius[1]), 0);
		// upper right corner
		theClip.curveTo(theClip.innerWidth, 0, theClip.innerWidth, cornerRadius[1]);
		// right side
		theClip.lineTo(theClip.innerWidth, (theClip.innerHeight-cornerRadius[2]));
		// bottom right corner
		theClip.curveTo(theClip.innerWidth, theClip.innerHeight, (theClip.innerWidth-cornerRadius[2]), theClip.innerHeight);
		// bottom side
		theClip.lineTo(cornerRadius[3], theClip.innerHeight);
		// bottom left corner
		theClip.curveTo(0, theClip.innerHeight, 0, (theClip.innerHeight-cornerRadius[3]));
		// left side
		theClip.lineTo(0, cornerRadius[0]);
		theClip.endFill();
	}
	private function toString():String {
		return className_str + " ("+version_str+")";
	}
}
